package common

import (
	"Hypnos-Soundcore/components/config"
	"Hypnos-Soundcore/components/ginparse"
	"Hypnos-Soundcore/components/helper"
	"Hypnos-Soundcore/constants"
	"github.com/gin-gonic/gin"
)

// BaseController base controller
type BaseController struct {
	Tag string
}

// InitEngineV1 init router
func (local *BaseController) InitEngineV1(e1 *gin.Engine) {
	_ = config.GetXConfig()
}

func init() {
	//// 异步事件进行上传文件操作
	//if err := eventbus.GetEventBus().SubscribeAsync(EventBusUploadAndUpdate,
	//	doUploadFiles, false); err != nil {
	//	helper.ErrorPrintf(constants.TagControllers, "EventBus subscribe failed...[%s]\n", err.Error())
	//}
}

// InitRouterGroupV1 init router
func (local *BaseController) InitRouterGroupV1(g *gin.RouterGroup) {
}

// InitEngineV1 init router
func (local *BaseController) InitEngineV2(e *gin.Engine) {
	_ = config.GetXConfig()

}

// InitRouterGroupV1 init router
func (local *BaseController) InitRouterGroupV2(g *gin.RouterGroup) {
}

// ParseAndCheckQuery 从url的query参数中解析入参
func (local *BaseController) ParseAndCheckQuery(c *gin.Context, obj interface{}) {
	if err := ginparse.ParseAndCheckQuery(c, obj); err != nil {
		local.PanicErrorResponse(c, constants.CodeInputParamInvalid, err, obj)
	}
}

// ParsePresetHeaders 从Header中解析数据
func (local *BaseController) ParsePresetHeaders(c *gin.Context, obj interface{}) {
	ginparse.ParsePresetHeaders(c, obj)
}

// ParseAndCheckRequest 从请求的body中解析入参
func (local *BaseController) ParseAndCheckRequest(c *gin.Context, obj interface{}) {
	if err := ginparse.ParseAndCheckRequest(c, obj); err != nil {
		local.PanicErrorResponse(c, constants.CodeInputParamInvalid, err, obj)
	}
}

// ParseAndCheckFormData 从请求的form中解析入参
func (local *BaseController) ParseAndCheckFormData(c *gin.Context, obj interface{}) {
	if err := ginparse.ParseAndCheckFormData(c, obj); err != nil {
		local.PanicErrorResponse(c, constants.CodeInputParamInvalid, err, obj)
	}
}

// ParseAndCheckSimpleForm 从请求的指定的一个form字段中解析入参
func (local *BaseController) ParseAndCheckSimpleForm(c *gin.Context, field string, obj interface{}) {
	if err := ginparse.ParseAndCheckSimpleForm(c, field, obj); err != nil {
		local.PanicErrorResponse(c, constants.CodeInputParamInvalid, err, obj)
	}
}

// PanicErrorResponse 为请求返回处理错误响应数据
func (local *BaseController) PanicErrorResponse(c *gin.Context, code int, err error, obj interface{}) {
	var message string
	if err != nil {
		message = err.Error()
	}
	SaveUserLogToFileV1(c, code, message, obj)
	// 避免返回明显的错误信息
	lang := c.GetHeader("Language")
	helper.PanicResponseByLanguageV1(code, lang)
}

// CreateUploadToken 生成上传凭证
//func (local *BaseController) CreateUploadToken(key string, contentLen int64, disableSSL bool) (ret string, err error) {
//	return awstools.GetS3Tool().CreateUploadToken(key, contentLen, disableSSL)
//}
//
//// AssumeRole 获取临时凭证
//func (local *BaseController) AssumeRole(authUser string, key string) (data *awstools.Token) {
//	awstools.GetStsTool().SetSessionName(authUser)
//	awstools.GetStsTool().SetUserDir(key)
//	return awstools.GetStsTool().AssumeRole()
//}

// GetAuthUser 获取当前登录用户
func (local *BaseController) GetHeaderAppName(c *gin.Context) string {
	return c.GetHeader(constants.HeaderAppName)
}

// ErrorResponse 为请求返回处理错误响应数据
func (local *BaseController) ErrorResponse(c *gin.Context, code int, err error, obj interface{}) {
	var message string
	if err != nil {
		message = err.Error()
	}
	SaveUserLogToFileV1(c, code, message, obj)
	// 避免返回明显的错误信息
	lang := c.GetHeader("Language")
	helper.PanicResponseByLanguageV1(code, lang)
}
